﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.International.Converters;
using Microsoft.International.Converters.PinYinConverter;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Globalization;
namespace TaoBaoKe.Common
{
   public static class StringExtension
    {
        /// <summary>
        /// 字符转拼音
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToPinYin(this string str)
        {
            if (str == null)
            {
                return "";
            }
            string tempStr = "";
            string tempPY = "";
            foreach (char tempChar in str)
            {
                if (ChineseChar.IsValidChar(tempChar))
                {
                    ChineseChar chConverter = new ChineseChar(tempChar);
                    tempPY = chConverter.Pinyins[0];
                   // tempPY = tempPY.Substring(0, tempPY.Length - 1);
                    tempPY = tempPY.Substring(0, 1);

                    tempStr += tempPY;
                }
                else
                {
                    if (tempChar!=' '&&tempChar!='/'&&tempChar!='.')
                    {
                        tempStr += tempChar;
                    }
                   
                }
            }
            return tempStr.ToLower();
        }
       /// <summary>
       /// 正则匹配
       /// </summary>
       /// <param name="str"></param>
       /// <param name="pattern"></param>
       /// <returns></returns>
        public static MatchCollection Matches(this string str,string pattern)
        {
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(str);
            
            return mc;
        }
        public static Match Match(this string str, string pattern)
        {
            Regex regex = new Regex(pattern);
            Match mc = regex.Match(str);

            return mc;
        }

        public static bool IsMatch(this string str, string pattern)
        {
            Regex regex = new Regex(pattern);
            return regex.IsMatch(str);
        }
       /// <summary>
       /// 64编码字符串
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
        public static string Base64Encode(this string str) {
            if (string.IsNullOrWhiteSpace(str))
            {
                return "";
            }
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
        }
       /// <summary>
       /// 64编码解码
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
        public static string Base64Decode(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return "";
            }
            return Encoding.UTF8.GetString(Convert.FromBase64CharArray(str.ToArray(), 0, str.Length));
        }

        public static string UrlEncode(this string str) {
            return System.Web.HttpUtility.UrlEncode(str);
        }

        public static string UnicodeEncode(this string str) {
            byte[] bts = Encoding.Unicode.GetBytes(str);
            string r = "";
            for (int i = 0; i < bts.Length; i += 2) r += "\\u" + bts[i + 1].ToString("x").PadLeft(2, '0') + bts[i].ToString("x").PadLeft(2, '0');
            return r;
        }

        public static string UnicodeDecode(this string str)
        {
            string r = "";
            MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            byte[] bts = new byte[2];
            foreach (Match m in mc)
            {
                bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
                bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
                r += Encoding.Unicode.GetString(bts);
            }
            return r;
        }

        public static string RemoveHTML(this string str) {
            Regex r=new Regex("<.*?>");
            string str1 = r.Replace(str, "");
            return str1;
        }

       /// <summary>
       /// 字母变为数字
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
        public static string CharToInt(this string str)
        {
            string temp = "";
            char[] chars = str.ToArray();
            for (int i = 0; i < chars.Length; i++)
            {
                temp += (int)chars[i] - 97;
            }
            return temp;
        }

       /// <summary>
       /// 数字变字母
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
        public static string IntToChar(this string str) {
            string temp = "";
            char[] chars = str.ToArray();
            for (int i = 0; i < chars.Length; i++)
            {
                int dd =int.Parse(chars[i].ToString());
                char d1 = (char)(dd + 97);
                temp +=Convert.ToString(d1);
            }
            return temp;
        }
    }
}
